package com.example.graduatetest.view.model;

import android.app.Activity;
import android.os.Handler;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.ViewModel;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.graduatetest.R;
import com.example.graduatetest.adapter.ClassAdapter;
import com.example.graduatetest.adapter.NoticeAdapter;
import com.example.graduatetest.bean.Notice;
import com.example.graduatetest.bean.User;
import com.example.graduatetest.callback.NoticeListCallBack;
import com.example.graduatetest.databinding.FragmentNoticeBinding;
import com.example.graduatetest.model.NoticeModel;
import com.example.graduatetest.utils.Constant;

import java.util.List;

public class NoticeViewModel extends ViewModel {
    private Activity activity;
    private FragmentNoticeBinding binding;
    private List<Notice> mNoticeList;
    private Handler handler = new Handler();
    private User user;
    public void init(Activity activity,FragmentNoticeBinding binding){
        this.activity = activity;
        this.binding = binding;
        user = Constant.checkStudentOrTeacher(activity);
        toolbarInit();
        getData();
    }


    //初始课程
    private void toolbarInit(){
        Toolbar toolbar = binding.toolbar16;
        toolbar.setTitle("消息通知");
    }

    //初始化recyclerView
    private void recyclerInit(){
        LinearLayoutManager layoutManager = new LinearLayoutManager(activity.getApplication());
        binding.recyclerViewNotice.setLayoutManager(layoutManager);
        if(mNoticeList != null){
            NoticeAdapter noticeAdapter = new NoticeAdapter(mNoticeList,activity);
            binding.recyclerViewNotice.setAdapter(noticeAdapter);
        }

    }

    private void getData(){
        NoticeModel.selectNoticeListByUser(user.getNumber(), new NoticeListCallBack() {
            @Override
            public void onSuccess(List<Notice> noticeList) {
                mNoticeList = noticeList;
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        recyclerInit();
                    }
                });
            }

            @Override
            public void onFailed() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(activity,"数据获取失败",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
}
